Python <<EOF
xterm_colors = {
  "000000": "x016_Grey0",
  "00005f": "x017_NavyBlue",
  "000087": "x018_DarkBlue",
  "0000af": "x019_Blue3",
  "0000d7": "x020_Blue3",
  "0000ff": "x021_Blue1",
  "005f00": "x022_DarkGreen",
  "005f5f": "x023_DeepSkyBlue4",
  "005f87": "x024_DeepSkyBlue4",
  "005faf": "x025_DeepSkyBlue4",
  "005fd7": "x026_DodgerBlue3",
  "005fff": "x027_DodgerBlue2",
  "008700": "x028_Green4",
  "00875f": "x029_SpringGreen4",
  "008787": "x030_Turquoise4",
  "0087af": "x031_DeepSkyBlue3",
  "0087d7": "x032_DeepSkyBlue3",
  "0087ff": "x033_DodgerBlue1",
  "00af00": "x034_Green3",
  "00af5f": "x035_SpringGreen3",
  "00af87": "x036_DarkCyan",
  "00afaf": "x037_LightSeaGreen",
  "00afd7": "x038_DeepSkyBlue2",
  "00afff": "x039_DeepSkyBlue1",
  "00d700": "x040_Green3",
  "00d75f": "x041_SpringGreen3",
  "00d787": "x042_SpringGreen2",
  "00d7af": "x043_Cyan3",
  "00d7d7": "x044_DarkTurquoise",
  "00d7ff": "x045_Turquoise2",
  "00ff00": "x046_Green1",
  "00ff5f": "x047_SpringGreen2",
  "00ff87": "x048_SpringGreen1",
  "00ffaf": "x049_MediumSpringGreen",
  "00ffd7": "x050_Cyan2",
  "00ffff": "x051_Cyan1",
  "5f0000": "x052_DarkRed",
  "5f005f": "x053_DeepPink4",
  "5f0087": "x054_Purple4",
  "5f00af": "x055_Purple4",
  "5f00d7": "x056_Purple3",
  "5f00ff": "x057_BlueViolet",
  "5f5f00": "x058_Orange4",
  "5f5f5f": "x059_Grey37",
  "5f5f87": "x060_MediumPurple4",
  "5f5faf": "x061_SlateBlue3",
  "5f5fd7": "x062_SlateBlue3",
  "5f5fff": "x063_RoyalBlue1",
  "5f8700": "x064_Chartreuse4",
  "5f875f": "x065_DarkSeaGreen4",
  "5f8787": "x066_PaleTurquoise4",
  "5f87af": "x067_SteelBlue",
  "5f87d7": "x068_SteelBlue3",
  "5f87ff": "x069_CornflowerBlue",
  "5faf00": "x070_Chartreuse3",
  "5faf5f": "x071_DarkSeaGreen4",
  "5faf87": "x072_CadetBlue",
  "5fafaf": "x073_CadetBlue",
  "5fafd7": "x074_SkyBlue3",
  "5fafff": "x075_SteelBlue1",
  "5fd700": "x076_Chartreuse3",
  "5fd75f": "x077_PaleGreen3",
  "5fd787": "x078_SeaGreen3",
  "5fd7af": "x079_Aquamarine3",
  "5fd7d7": "x080_MediumTurquoise",
  "5fd7ff": "x081_SteelBlue1",
  "5fff00": "x082_Chartreuse2",
  "5fff5f": "x083_SeaGreen2",
  "5fff87": "x084_SeaGreen1",
  "5fffaf": "x085_SeaGreen1",
  "5fffd7": "x086_Aquamarine1",
  "5fffff": "x087_DarkSlateGray2",
  "870000": "x088_DarkRed",
  "87005f": "x089_DeepPink4",
  "870087": "x090_DarkMagenta",
  "8700af": "x091_DarkMagenta",
  "8700d7": "x092_DarkViolet",
  "8700ff": "x093_Purple",
  "875f00": "x094_Orange4",
  "875f5f": "x095_LightPink4",
  "875f87": "x096_Plum4",
  "875faf": "x097_MediumPurple3",
  "875fd7": "x098_MediumPurple3",
  "875fff": "x099_SlateBlue1",
  "878700": "x100_Yellow4",
  "87875f": "x101_Wheat4",
  "878787": "x102_Grey53",
  "8787af": "x103_LightSlateGrey",
  "8787d7": "x104_MediumPurple",
  "8787ff": "x105_LightSlateBlue",
  "87af00": "x106_Yellow4",
  "87af5f": "x107_DarkOliveGreen3",
  "87af87": "x108_DarkSeaGreen",
  "87afaf": "x109_LightSkyBlue3",
  "87afd7": "x110_LightSkyBlue3",
  "87afff": "x111_SkyBlue2",
  "87d700": "x112_Chartreuse2",
  "87d75f": "x113_DarkOliveGreen3",
  "87d787": "x114_PaleGreen3",
  "87d7af": "x115_DarkSeaGreen3",
  "87d7d7": "x116_DarkSlateGray3",
  "87d7ff": "x117_SkyBlue1",
  "87ff00": "x118_Chartreuse1",
  "87ff5f": "x119_LightGreen",
  "87ff87": "x120_LightGreen",
  "87ffaf": "x121_PaleGreen1",
  "87ffd7": "x122_Aquamarine1",
  "87ffff": "x123_DarkSlateGray1",
  "af0000": "x124_Red3",
  "af005f": "x125_DeepPink4",
  "af0087": "x126_MediumVioletRed",
  "af00af": "x127_Magenta3",
  "af00d7": "x128_DarkViolet",
  "af00ff": "x129_Purple",
  "af5f00": "x130_DarkOrange3",
  "af5f5f": "x131_IndianRed",
  "af5f87": "x132_HotPink3",
  "af5faf": "x133_MediumOrchid3",
  "af5fd7": "x134_MediumOrchid",
  "af5fff": "x135_MediumPurple2",
  "af8700": "x136_DarkGoldenrod",
  "af875f": "x137_LightSalmon3",
  "af8787": "x138_RosyBrown",
  "af87af": "x139_Grey63",
  "af87d7": "x140_MediumPurple2",
  "af87ff": "x141_MediumPurple1",
  "afaf00": "x142_Gold3",
  "afaf5f": "x143_DarkKhaki",
  "afaf87": "x144_NavajoWhite3",
  "afafaf": "x145_Grey69",
  "afafd7": "x146_LightSteelBlue3",
  "afafff": "x147_LightSteelBlue",
  "afd700": "x148_Yellow3",
  "afd75f": "x149_DarkOliveGreen3",
  "afd787": "x150_DarkSeaGreen3",
  "afd7af": "x151_DarkSeaGreen2",
  "afd7d7": "x152_LightCyan3",
  "afd7ff": "x153_LightSkyBlue1",
  "afff00": "x154_GreenYellow",
  "afff5f": "x155_DarkOliveGreen2",
  "afff87": "x156_PaleGreen1",
  "afffaf": "x157_DarkSeaGreen2",
  "afffd7": "x158_DarkSeaGreen1",
  "afffff": "x159_PaleTurquoise1",
  "d70000": "x160_Red3",
  "d7005f": "x161_DeepPink3",
  "d70087": "x162_DeepPink3",
  "d700af": "x163_Magenta3",
  "d700d7": "x164_Magenta3",
  "d700ff": "x165_Magenta2",
  "d75f00": "x166_DarkOrange3",
  "d75f5f": "x167_IndianRed",
  "d75f87": "x168_HotPink3",
  "d75faf": "x169_HotPink2",
  "d75fd7": "x170_Orchid",
  "d75fff": "x171_MediumOrchid1",
  "d78700": "x172_Orange3",
  "d7875f": "x173_LightSalmon3",
  "d78787": "x174_LightPink3",
  "d787af": "x175_Pink3",
  "d787d7": "x176_Plum3",
  "d787ff": "x177_Violet",
  "d7af00": "x178_Gold3",
  "d7af5f": "x179_LightGoldenrod3",
  "d7af87": "x180_Tan",
  "d7afaf": "x181_MistyRose3",
  "d7afd7": "x182_Thistle3",
  "d7afff": "x183_Plum2",
  "d7d700": "x184_Yellow3",
  "d7d75f": "x185_Khaki3",
  "d7d787": "x186_LightGoldenrod2",
  "d7d7af": "x187_LightYellow3",
  "d7d7d7": "x188_Grey84",
  "d7d7ff": "x189_LightSteelBlue1",
  "d7ff00": "x190_Yellow2",
  "d7ff5f": "x191_DarkOliveGreen1",
  "d7ff87": "x192_DarkOliveGreen1",
  "d7ffaf": "x193_DarkSeaGreen1",
  "d7ffd7": "x194_Honeydew2",
  "d7ffff": "x195_LightCyan1",
  "ff0000": "x196_Red1",
  "ff005f": "x197_DeepPink2",
  "ff0087": "x198_DeepPink1",
  "ff00af": "x199_DeepPink1",
  "ff00d7": "x200_Magenta2",
  "ff00ff": "x201_Magenta1",
  "ff5f00": "x202_OrangeRed1",
  "ff5f5f": "x203_IndianRed1",
  "ff5f87": "x204_IndianRed1",
  "ff5faf": "x205_HotPink",
  "ff5fd7": "x206_HotPink",
  "ff5fff": "x207_MediumOrchid1",
  "ff8700": "x208_DarkOrange",
  "ff875f": "x209_Salmon1",
  "ff8787": "x210_LightCoral",
  "ff87af": "x211_PaleVioletRed1",
  "ff87d7": "x212_Orchid2",
  "ff87ff": "x213_Orchid1",
  "ffaf00": "x214_Orange1",
  "ffaf5f": "x215_SandyBrown",
  "ffaf87": "x216_LightSalmon1",
  "ffafaf": "x217_LightPink1",
  "ffafd7": "x218_Pink1",
  "ffafff": "x219_Plum1",
  "ffd700": "x220_Gold1",
  "ffd75f": "x221_LightGoldenrod2",
  "ffd787": "x222_LightGoldenrod2",
  "ffd7af": "x223_NavajoWhite1",
  "ffd7d7": "x224_MistyRose1",
  "ffd7ff": "x225_Thistle1",
  "ffff00": "x226_Yellow1",
  "ffff5f": "x227_LightGoldenrod1",
  "ffff87": "x228_Khaki1",
  "ffffaf": "x229_Wheat1",
  "ffffd7": "x230_Cornsilk1",
  "ffffff": "x231_Grey100",
  "080808": "x232_Grey3",
  "121212": "x233_Grey7",
  "1c1c1c": "x234_Grey11",
  "262626": "x235_Grey15",
  "303030": "x236_Grey19",
  "3a3a3a": "x237_Grey23",
  "444444": "x238_Grey27",
  "4e4e4e": "x239_Grey30",
  "585858": "x240_Grey35",
  "626262": "x241_Grey39",
  "6c6c6c": "x242_Grey42",
  "767676": "x243_Grey46",
  "808080": "x244_Grey50",
  "8a8a8a": "x245_Grey54",
  "949494": "x246_Grey58",
  "9e9e9e": "x247_Grey62",
  "a8a8a8": "x248_Grey66",
  "b2b2b2": "x249_Grey70",
  "bcbcbc": "x250_Grey74",
  "c6c6c6": "x251_Grey78",
  "d0d0d0": "x252_Grey82",
  "dadada": "x253_Grey85",
  "e4e4e4": "x254_Grey89",
  "eeeeee": "x255_Grey93",
}

def rgb_distance(rgb1, rgb2):
  r = abs(int(rgb1[:2], 16) - int(rgb2[:2], 16))
  g = abs(int(rgb1[2:4], 16) - int(rgb2[2:4], 16))
  b = abs(int(rgb1[4:6], 16) - int(rgb2[4:6], 16))
  return r + g + b

def rgb_to_xterm(rgb):
  curDist = float("inf")
  # Failsafe
  curColor = "x242_Grey42"

  for key in xterm_colors:
    dist = rgb_distance(key, rgb)
    if dist < curDist:
      curDist = dist
      curColor = xterm_colors[key]

  return curColor
EOF

function! gh_colors#init()
  " stub to load the colors
  let g:gissues_xterm_colors = 1
endfunction
